/*
 * Copyright (C) 2020, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <QTime>
#include <QDesktopServices>
#include <QUrl>
#include "menumodule.h"
#include "config_ip_address_dialog.h"
#include "size_para.h"
#include "gsettings_monitor.h"
#include "custom_about_dialog.h"

MenuModule::MenuModule(QWidget *parent)
  : QWidget(parent),
    m_mainWin(parent)
{
    init();
}

void MenuModule::init()
{
    initAction();
    setStyle();
}

void MenuModule::initAction()
{
    m_menuButton = new QToolButton(this);
    m_menuButton->setProperty("isWindowButton", 0x1);
    m_menuButton->setProperty("useIconHighlightEffect", 0x2);
    m_menuButton->setPopupMode(QToolButton::InstantPopup);
    m_menuButton->setFixedSize(30,30);
    m_menuButton->setIconSize(QSize(16, 16));
    m_menuButton->setAutoRaise(true);
    m_menuButton->setIcon(QIcon::fromTheme("open-menu-symbolic"));

    m_menu = new QMenu();

    QList<QAction *> actions ;

    QAction *actionHelp = new QAction(m_menu);
    actionHelp->setText(tr("Help"));
    QAction *actionAbout = new QAction(m_menu);
    actionAbout->setText(tr("About"));
    QAction *actionConfig = new QAction(m_menu);
    actionConfig->setText(tr("Configure"));
    QAction *actionQuit = new QAction(m_menu);
    actionQuit->setText(tr("Quit"));
    actions << actionConfig
            << actionAbout
            << actionHelp
            << actionQuit;

    m_menu->addActions(actions);

    m_menuButton->setMenu(m_menu);
    connect(m_menu,&QMenu::triggered,this,&MenuModule::triggerMenu);
    initGsetting();
    setStyleByThemeGsetting();
}


void MenuModule::setStyleByThemeGsetting(){
    QString nowThemeStyle = GsettingsMonitor::getInstance()->sysStyleNameGet();
    if("ukui-dark" == nowThemeStyle || "ukui-black" == nowThemeStyle)
    {
        setThemeDark();
    }else{
        setThemeLight();
    }
}

void MenuModule::triggerMenu(QAction *act)
{
    QString str = act->text();
    if(tr("Quit") == str){
        qDebug() << "从menu中的quit退出程序！";
        emit menuModuleClose();
    }
    else if(tr("About") == str){
        aboutAction();
    }
    else if(tr("Help") == str){
        helpAction();
    }
    else if(tr("Configure") == str){
        ConfigIPAddressDialog configDialog(m_mainWin);
        if(configDialog.getConfigStatus())
        {
            configDialog.show();
            configDialog.exec();
        }
        else
        {
            return;
        }
    }
}

void MenuModule::aboutAction()
{
    CustomAboutDialog dialog(m_mainWin);
    dialog.setAppName(m_appShowingName);
    dialog.setAppIcon(QIcon::fromTheme("kylin-printer"));
    dialog.setAppVersion("1.3.0");
    dialog.exec();
}

void MenuModule::helpAction()
{
//    帮助点击事件处理

    DaemonIpcDbus *ipcDbus = DaemonIpcDbus::getInstance();
    if(!ipcDbus->daemonIsNotRunning()){
        ipcDbus->showGuide(m_appName);
    }
}

void MenuModule::setStyle()
{
    m_menuButton->setObjectName("menuButton");
    // qDebug() << "menuButton->styleSheet" << menuButton->styleSheet();
    m_menuButton->setStyleSheet("QPushButton::menu-indicator{image:None;}");
}

void MenuModule::initGsetting(){
    if(!GsettingsMonitor::getInstance()->gsettingsCheck()){
        return ;
    }
    connect(GsettingsMonitor::getInstance(), &GsettingsMonitor::sysStyleNameUpdate,
            this,                          &MenuModule::refreshThemeBySystemConf);

}

void MenuModule::refreshThemeBySystemConf()
{
    QString themeNow = GsettingsMonitor::getInstance()->sysStyleNameGet();
    if("ukui-dark" == themeNow || "ukui-black" == themeNow){
        setThemeDark();
    }else{
        setThemeLight();
    }
}

void MenuModule::setThemeDark()
{

}

void MenuModule::setThemeLight()
{

}

void MenuModule::setQSSFontSize(QFont curFont)
{
    //带QSS的控件设置字体ui->xx->setFont(font);
    double Font14Size = curFont.pointSizeF()/15*14;
    QFont font14 = curFont;
    font14.setPointSizeF(Font14Size);

    m_menu->setFont(font14);
}
